package com.example.likegifmarker.controller.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.example.likegifmarker.Model.Beans.EventStub;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;

/*
 * Created by　Dullyoung on 2020/12/14
 */
public abstract class BaseFragment extends Fragment {
    protected View mRootView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            mRootView = View.inflate(getActivity(), getLayoutId(), null);
            ButterKnife.bind(this, mRootView);

            initViews();
            if (!EventBus.getDefault().isRegistered(this)) {
                EventBus.getDefault().register(this);
            }
        }
        return mRootView;
    }

    protected abstract int getLayoutId();

    protected abstract void initViews();
    // eventbus stub
    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void onStub(EventStub stub) {
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
